package cn.hznc.controller.production;

import cn.hutool.core.util.ObjectUtil;
import cn.hznc.aspect.BusinessType;
import cn.hznc.aspect.Log;
import cn.hznc.controller.production.strategy.AbutmentProductionContext;
import cn.hznc.controller.production.strategy.ZccAbutmentProductionStrategy;
import cn.hznc.controller.service.order.AbutmentService;
import cn.hznc.domain.entity.ProductionEntity;
import cn.hznc.domain.request.process.OperateBomReq;
import cn.hznc.domain.request.production.AddOrEditProductionReq;
import cn.hznc.domain.request.production.QueryProductionReq;
import cn.hznc.domain.response.PageResult;
import cn.hznc.domain.response.ResponseBody;
import cn.hznc.exception.ToolManageException;
import cn.hznc.controller.service.production.ProductionManageService;
import cn.hznc.mapper.ProductionMapper;
import cn.hznc.permission.Permission;
import cn.hznc.utils.CommonUtil;
import cn.hznc.utils.FileUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 产品管理页面
 *
 * @Author Administrator
 * @Date 2023/3/13 16:00
 */
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/production")
@Slf4j
public class ProductionManageController {
    @Resource
    private ProductionManageService productionManageService;

    @Resource
    private AbutmentService abutmentService;

    @Resource
    private ProductionMapper productionMapper;

    @Resource
    private ZccAbutmentProductionStrategy zccAbutmentProductionStrategy;

    /**
     * 产品列表查询
     *
     * @param queryProductionReq queryProductionReq
     * @return
     */
    @RequestMapping(value = "/queryProductionList", method = RequestMethod.POST)
    @Permission(title = "产品列表查询" , permissions = {"production.read"})
    public ResponseBody<PageResult<ProductionEntity>> queryProductionList(@RequestBody QueryProductionReq queryProductionReq) {
        return ResponseBody.success(productionManageService.queryProductionList(queryProductionReq));
    }

    /**
     * 产品列表查询
     *
     * @param addOrEditProductionReq addOrEditProductionReq
     * @return
     */
    @RequestMapping(value = "/addOrEditProduction", method = RequestMethod.POST)
    @Log(title = "新增或编辑产品", businessType = BusinessType.OTHER)
    @Permission(title = "产品列表新增或编辑" , permissions = {"production.add" , "production.update"})
    public ResponseBody<Boolean> addOrEditProduction(@RequestBody AddOrEditProductionReq addOrEditProductionReq) {
        try {
            productionManageService.addOrEditProduction(addOrEditProductionReq);
        } catch (ToolManageException te) {
            return ResponseBody.error(te.getCode(), te.getMsg());
        }
        return ResponseBody.success(Boolean.TRUE);
    }

    /**
     * 关联数据
     *
     * @return
     */
    @RequestMapping(value = "/abutmentDataConnect", method = RequestMethod.GET)
    public ResponseBody<Boolean> abutmentDataConnect(@RequestParam("materialNo") String materialNo) {
        QueryWrapper<ProductionEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("material_no", materialNo);
        ProductionEntity productionEntity = productionMapper.selectOne(wrapper);
        if (ObjectUtil.isNotNull(productionEntity)) {
            abutmentService.dataConnect(Arrays.asList(productionEntity));
        }
        return ResponseBody.success(Boolean.TRUE);
    }


    /**
     * 新增修改删除物料
     *
     * @param operateBomReq operateBomReq
     * @return
     */
    @RequestMapping(value = "/editOrAddBom", method = RequestMethod.POST)
    @Log(title = "新增或编辑bom信息", businessType = BusinessType.OTHER)
    public ResponseBody<Boolean> editOrAddBom(@RequestBody OperateBomReq operateBomReq) {
        try {
            productionManageService.editOrAddBom(operateBomReq);
        } catch (ToolManageException te) {
            return ResponseBody.error(te.getCode(), te.getMsg());
        }
        return ResponseBody.success(Boolean.TRUE);
    }

    /**
     * 修改产品物料状态
     *
     * @return
     */
    @RequestMapping(value = "/editBomStatus", method = RequestMethod.GET)
    @Log(title = "编辑物料bom状态", businessType = BusinessType.UPDATE)
    @Permission(title = "编辑产品bom状态", permissions = {"productiondetailstatus.update"})
    public ResponseBody<Boolean> editBomStatus(@RequestParam String materialNo, @RequestParam Integer status) {
        try {
            productionManageService.editBomStatus(materialNo, status);
        } catch (ToolManageException te) {
            return ResponseBody.error(te.getCode(), te.getMsg());
        }
        return ResponseBody.success(Boolean.TRUE);
    }

    /**
     * 查询产品物料状态
     *
     * @return
     */
    @RequestMapping(value = "/queryBomStatus", method = RequestMethod.GET)
    public ResponseBody queryBomStatus(@RequestParam String materialNo) {
        try {
            return ResponseBody.success(productionManageService.queryBomStatus(materialNo));
        } catch (ToolManageException te) {
            return ResponseBody.error(te.getCode(), te.getMsg());
        }
    }

    /**
     * 新增修改删除物料
     *
     * @return
     */
    @RequestMapping(value = "/queryBomDetail", method = RequestMethod.GET)
    public ResponseBody queryBomDetail(@RequestParam("figureNo") String figureNo, @RequestParam("version") Integer version) {
        return ResponseBody.success(productionManageService.queryBomDetail(figureNo, version));
    }


    /**
     * 产品删除
     *
     * @param ids ids
     * @return
     */
    @RequestMapping(value = "/deleteProduction", method = RequestMethod.POST)
    @Log(title = "删除产品", businessType = BusinessType.DELETE)
    @Permission(title = "删除产品" , permissions = {"production.delete"})
    public ResponseBody<Boolean> deleteProduction(@RequestBody List<Integer> ids) {
        try {
            productionManageService.deleteProduction(ids);
        } catch (ToolManageException te) {
            log.error("deleteProduction error======>{}", te.getMsg());
            ResponseBody.error(te.getCode(), te.getMsg());
        }
        return ResponseBody.success(Boolean.TRUE);
    }

    /**
     * 产品图片上传
     *
     * @return
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public ResponseBody<Map<String, Object>> upload(@RequestPart("file") MultipartFile file,
                                                    @RequestPart("materialNo") String materialNo) {
        try {
            Map<String, Object> retMap = productionManageService.upload(file, materialNo);
            return ResponseBody.success(retMap);
        } catch (ToolManageException e) {
            return ResponseBody.error(e.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/showProcessState", method = RequestMethod.GET)
    public ResponseBody showProcessState(@RequestParam("materialNo") String materialNo) {
        return ResponseBody.success(productionManageService.showPeocessStates(materialNo));
    }

    @RequestMapping(value = "/unbindbyFigureNo", method = RequestMethod.GET)
    @Permission(title = "产品关联工艺" , permissions = {"pucAndPro.connect"})
    public ResponseBody unbindbyFigureNo(@RequestParam("materialNo") String materialNo) {
        productionManageService.unbind(materialNo);
        return ResponseBody.success();
    }

    @RequestMapping(value = "/unbindbyCode", method = RequestMethod.GET)
    public ResponseBody unbindbyCode(@RequestParam("code") String code) {
        return ResponseBody.success(productionManageService.selectBycode(code));
    }

    @RequestMapping(value = "/abutmentProduction", method = RequestMethod.GET)
    public ResponseBody abutmentProduction(@RequestParam("materialNos") List<String> materialNos) {
        AbutmentProductionContext abutmentProductionContext = new AbutmentProductionContext(zccAbutmentProductionStrategy);
        abutmentProductionContext.abutmentProduction(materialNos);
        return ResponseBody.success();
    }

    /**
     * 导出产品excel模版
     *
     * @param response
     */
    @RequestMapping(value = "/exportProductionTemplate", method = RequestMethod.GET)
    public void exportProductionTemplate(HttpServletResponse response) {
        productionManageService.exportProductionTemplate(response);
    }

    /**
     * 导入产品excel模版
     */
    @RequestMapping(value = "/importProductionTemplate", method = RequestMethod.POST)
    public ResponseBody importProductionTemplate(@RequestPart("file") MultipartFile file) {
        List<List<LinkedHashMap>> data = CommonUtil.uploadZipFile(file);
        productionManageService.importProductionTemplate(data);
        return ResponseBody.success();

    }
}
